﻿using Project.API.Models.information;
using Project.BLL.information;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Project.API.Controllers.information
{
    public class user_infoController : ApiController
    {
        // GET: api/user_info
        [HttpGet]
        public ResultPageList<user_infoDto> Get(int page, int limit, string name = "")
        {
            int count = 0;
            var list = new user_infoBll().GetUser_Infos(page, limit, name, out count);
            var data = from u in list
                       select new user_infoDto
                       {
                           user_id = u.user_id,
                           username = u.username,
                           password_hash = u.password_hash,
                           mobile = u.mobile,
                           email = u.email,
                           address = u.address,
                           points = (int)u.points,
                           membership_level = (byte)u.membership_level,
                           create_time = (DateTime)u.create_time,
                           last_login_time = (DateTime)u.last_login_time,
                           is_delete = (bool)u.is_delete
                       };

            ResultPageList<user_infoDto> result = new ResultPageList<user_infoDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();
            return result;


        }


        // POST: api/user_info
        public Result Post([FromBody]user_infoDto v)
        {
            user_info u = new user_info();
            u.user_id = v.user_id;
            u.username = v.username;
            u.address = v.address;
            u.email = v.email;
            u.mobile = v.mobile;
            u.create_time = v.create_time;
            u.last_login_time = v.last_login_time;
            u.password_hash = v.password_hash;
            u.points = v.points;

            // 根据 points 的值设置 membership_level 的值
            if (u.points >= 0 && u.points <= 50)
            {
                u.membership_level = 0;
            }
            else if (u.points > 50 && u.points <= 100)
            {
                u.membership_level = 1;
            }
            else if (u.points > 100 && u.points <= 150)
            {
                u.membership_level = 2;
            }
            else
            {
                // 如果 points 不在上述范围内，可以根据需要设置默认值或进行其他处理
                u.membership_level = 0;
            }

            u.is_delete = v.is_delete;
            bool bol = new user_infoBll().Add(u);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息添加成功！" : "error";
            return result;

        }

        // PUT: api/user_info/5
        public Result Put( [FromBody]user_infoDto v)
        {
            user_info u = new user_info();
            u.user_id = v.user_id;
            u.username = v.username;
            u.address = v.address;
            u.email = v.email;
            u.mobile = v.mobile;
            u.last_login_time = v.last_login_time;
            u.create_time = v.create_time;
            u.points = v.points;
            u.is_delete = v.is_delete;
            u.password_hash = v.password_hash;


            if (u.points >= 0 && u.points <= 50)
            {
                u.membership_level = 0;
            }
            else if (u.points > 50 && u.points <= 100)
            {
                u.membership_level = 1;
            }
            else if (u.points > 100 && u.points <= 150)
            {
                u.membership_level = 2;
            }
            else
            {
                // 如果 points 不在上述范围内，可以根据需要设置默认值或进行其他处理
                u.membership_level = 0;
            }

            bool bol = new user_infoBll().Update(u);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户修改成功" : "error";
            return result;

        }

        // DELETE: api/user_info/5
        public Result Delete(int id)
        {
            bool bol=new user_infoBll().Del(id);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户删除成功" : "error";
            return result;
        }
        [HttpDelete]
        public Result Delete(int[] ids)
        {
            bool bol = new user_infoBll().Del(ids);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息删除成功！" : "出错了！";
            return result;
        }
    }
}
